﻿<html>
  <head>
    <title>Net.Socket - raw TCP connection socket</title>
    <meta name="generator" content="h-smile:richtext">
  </head>
<body>
  <h1>Net.Socket object</h1>
  <p>Sockets are used for duplex raw data transfer using either named pipes (Windows) or TCP/IP sockets between two Sciter processes running either on the same machine (&quot;localhost&quot; address) or over the wire. Socket supports server listening sockets (<code>Socket.listen()</code> ) and client sockets (<code>Socket.connect()</code>) .</p>
  <p>Socket sends/receive raw data - strings or bytes. You send data by <code>socket.send(data)</code> and receive them by <code>socket.on(&quot;receive&quot;, function(data) {})</code> on other end. </p>
  <dl>
    <h2>Properties</h2>
    <h2>Methods</h2>
    <dt>connect</dt>
    <dd>
      <p><strong>( port: </strong>integer [<strong>, domain</strong>: string] <strong>)</strong> : DataSocket</p>
      <p>Constructs client DataSocket. Returns new socket in connecting state.</p></dd>
    <dt>listen</dt>
    <dd>
      <p><strong>( acceptor</strong>: function,<strong> port: </strong>integer [, <strong>domain</strong>: string] <strong>)</strong> : DataSocket</p>
      <p>Constructs server DataSocket. Returns new socket in listening state.</p>
      <p>The <em>acceptor</em> function is being called on each new connection request to the server. It has following signature:</p>
      <pre>function acceptor( connectionSocket: DataSocket ) : true | false
</pre>
      <p>where <em>connectionSocket</em> is another instance of DataSocket used for communication with remote peer.</p>
      <p>You MUST return <em>true</em> from the acceptor in order to accept and use the connection.</p></dd>
    <dt>on</dt>
    <dd>
      <p><strong>( event</strong>: string<strong>, callback</strong>: function<strong> )</strong> : this</p>
      <p>Subscribes the callback to one of socket events:</p>
      <ul>
        <li><strong>&quot;connect&quot;</strong> -&nbsp;<code>function()</code>, socket just connected to the host;</li>
        <li><strong>&quot;data&quot;</strong> -&nbsp;<code>function(data:string)</code>, the data has been received;</li>
        <li><strong>&quot;sent&quot;</strong> -&nbsp;<code>function()</code>, data were sent in full;</li>
        <li><strong>&quot;error&quot;</strong> -&nbsp;<code>function(err: Error)</code>, error occurred, error object passed to the function;</li>
        <li><strong>&quot;close&quot;</strong> -&nbsp;<code>function()</code>, socket was closed;</li></ul>
      <p>The event name may contain &quot;.namespace&quot; part that can be used in .off() call.</p></dd>
    <dt>off</dt>
    <dd>
      <p><strong>( event</strong>: string | <strong>callback</strong>: function<strong> )</strong> : this</p>
      <p>Unsubscribe callback either by its name or by its function reference.</p>
      <p>Event name may contain only namespace part, so this: <code>socket.off(&quot;.namespace&quot;)</code> will unsubscribe all handlers that were set with that namespace.</p></dd>
    <dt>send</dt>
    <dd>
      <p><strong>( data</strong>: any <strong>)</strong></p>
      <p>The method sends data to the peer. The data can be any serializeable data type (object, number, string, array, etc.).</p></dd>
    <dt>close</dt>
    <dd>
      <p><strong>( )</strong></p>
      <p>Closes the socket.</p></dd></dl>
</body>
</html>